package com.dragon.modules.yuegou.client.wechat.request;

import com.dragon.modules.yuegou.client.wechat.model.XmlUtil;
import com.dragon.modules.yuegou.utils.MD5;
import com.dragon.modules.yuegou.utils.Tools;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 提现到银行卡
 */
public class PayBankRequest implements Serializable {
	private static final long serialVersionUID = 1L;
	private String url;

	// 商户号
	private String mchId;

	// 商户企业付款单号
	private String partnerTradeNo;

	// 随机字符串
	private String nonceStr;

	// 通过MD5签名算法计算得出的签名值，详见MD5签名生成算法
	private String sign;

	// 收款方银行卡号
	private String encBankNo;

	// 收款方用户名 收款方用户名（采用标准RSA算法，公钥由微信侧提供）详见获取RSA加密公钥API
	private String encTrueName;
	// 收款方开户行 银行卡所在开户行编号,详见银行编号列表
	private String bankCode;

	// 付款金额：RMB分（支付总额，不含手续费） 大于0的整数
	private Integer amount;

	// 付款说明
	private String desc;

	private String requestXml;

	private String md5Key;

	public PayBankRequest() {
		this.url = "https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank";
		this.mchId = "1521186421";
		this.md5Key = "F98F6B7VST47UJOO478ZDY9VB8W6O0SE";
		this.nonceStr = Tools.createRandom(false, 15);
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getPartnerTradeNo() {
		return partnerTradeNo;
	}

	public void setPartnerTradeNo(String partnerTradeNo) {
		this.partnerTradeNo = partnerTradeNo;
	}

	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getEncBankNo() {
		return encBankNo;
	}

	public void setEncBankNo(String encBankNo) {
		this.encBankNo = encBankNo;
	}

	public String getEncTrueName() {
		return encTrueName;
	}

	public void setEncTrueName(String encTrueName) {
		this.encTrueName = encTrueName;
	}

	public String getBankCode() {
		return bankCode;
	}

	public void setBankCode(String bankCode) {
		this.bankCode = bankCode;
	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getRequestXml() {
		return requestXml;
	}

	public void setRequestXml(String requestXml) {
		this.requestXml = requestXml;
	}

	public void sign() {
		SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
		parameters.put("mch_id", this.mchId);
		parameters.put("partner_trade_no", this.partnerTradeNo);
		parameters.put("nonce_str", this.nonceStr);
		parameters.put("enc_bank_no", this.encBankNo);
		parameters.put("enc_true_name", this.encTrueName);
		parameters.put("bank_code", this.bankCode);
		parameters.put("amount", this.amount);

		if (StringUtils.isNotBlank(this.desc)) {
			parameters.put("desc", this.desc);
		}

		String createSign = createSign(parameters, this.md5Key);
		this.sign = createSign;
	}

	public void requestXml() {
		TreeMap<String, Object> parameters = new TreeMap<String, Object>();
		parameters.put("mch_id", this.mchId);
		parameters.put("partner_trade_no", this.partnerTradeNo);
		parameters.put("nonce_str", this.nonceStr);
		parameters.put("enc_bank_no", this.encBankNo);
		parameters.put("enc_true_name", this.encTrueName);
		parameters.put("bank_code", this.bankCode);
		parameters.put("amount", this.amount);

		if (StringUtils.isNotBlank(this.desc)) {
			parameters.put("desc", this.desc);
		}
		if (StringUtils.isNotBlank(this.sign)) {
			parameters.put("sign", this.sign);
		} else {
			this.sign();
			parameters.put("sign", this.sign);
		}

		String requestXml = XmlUtil.converterWX(this.createWXPAYPostParam(parameters));

		this.requestXml = requestXml;
	}

	@SuppressWarnings("rawtypes")
	public static String createSign(SortedMap<Object, Object> parameters, String key) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序（升序）
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		String sign = MD5.wxsign(sb.toString(), key, "utf-8").toUpperCase();
		return sign;
	}

	@SuppressWarnings("rawtypes")
	private Map<String, Object> createWXPAYPostParam(Map<String, Object> parameters) {
		Map<String, Object> map = new TreeMap<String, Object>();
		Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序（升序）
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			map.put(k, v);
		}
		return map;
	}
}
